﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Maticsoft.Common;

namespace Maticsoft.Web
{
    public partial class UserAddrEdit : PageBaseMember
    {
        int _AddrId;
        protected void Page_Load( object sender, EventArgs e )
        {
            Check();
            UserLeft1.FlagId = "5";
            int.TryParse(Request.QueryString["id"], out _AddrId);
            if (!IsPostBack)
            {
                BindProvince();
                BindCity(0);
                GetData();
            }
        }

        protected void GetData( )
        {
            if (_AddrId > 0)
            {
                BLL.DeliveryAddr delBLL = new BLL.DeliveryAddr();
                Model.DeliveryAddr delModel = delBLL.GetModel(_AddrId);
                if (delModel.UserId != DigitHelper.GetInt32(this.UserId))
                {
                    return;
                }
                txtName.Text = delModel.Name;
                txtMobile.Text = delModel.Mobile;
                txtAddress.Text = delModel.Address;
                txtPostCode.Text = delModel.PostCode;
                BLL.LocationInfo locBLL = new BLL.LocationInfo();
                int locId = 0;
                int.TryParse(Convert.ToString(delModel.LocationId), out locId);
                Model.LocationInfo locModel = locBLL.GetModel(locId);
                if (locModel == null)
                {
                    locModel = new Model.LocationInfo();
                }
                ddlProvince.SelectedValue = Convert.ToString(locModel.pid);
                BindCity(DigitHelper.GetInt32(locModel.pid));
                ddlCity.SelectedValue = locId.ToString();
                txtIdentityNo.Text = delModel.IdentityNo;
                cbMain.Checked = DigitHelper.GetInt32(delModel.IsMain) == 1;
                if (!string.IsNullOrEmpty(delModel.IdentityFrontPic))
                {
                    imgFront.ImageUrl = "~/uploadpic/identity/" + delModel.IdentityFrontPic;
                }
                if (!string.IsNullOrEmpty(delModel.IdentityBackPic))
                {
                    imgBack.ImageUrl = "~/uploadpic/identity/" + delModel.IdentityBackPic;
                }
            }
        }

        protected void BindProvince( )
        {
            BLL.LocationInfo locBLL = new BLL.LocationInfo();
            List<Model.LocationInfo> list = locBLL.GetModelList("locationId<100");
            ddlProvince.DataSource = list;
            ddlProvince.DataTextField = "locationName";
            ddlProvince.DataValueField = "locationId";
            ddlProvince.DataBind();

            ddlProvince.Items.Insert(0, new ListItem("选择省", "-1"));
        }

        protected void BindCity( int provinceId )
        {
            BLL.LocationInfo locBLL = new BLL.LocationInfo();
            List<Model.LocationInfo> list = locBLL.GetModelList("pId=" + provinceId + " and locationId=cId");
            ddlCity.DataSource = list;
            ddlCity.DataTextField = "locationName";
            ddlCity.DataValueField = "locationId";
            ddlCity.DataBind();

            ddlCity.Items.Insert(0, new ListItem("选择市", "-1"));
        }

        protected void ddlProvince_SelectedIndexChanged( object sender, EventArgs e )
        {
            BindCity(int.Parse(ddlProvince.SelectedValue));
        }

        protected void btnSave_Click( object sender, EventArgs e )
        {
            BLL.DeliveryAddr delBLL = new BLL.DeliveryAddr();
            if (cbMain.Checked)
            {
                List<Model.DeliveryAddr> list = delBLL.GetModelList("UserId=" + this.UserId + " and IsMain=1");
                if (list.Count > 0)
                {
                    list[0].IsMain = 0;
                    delBLL.Update(list[0]);
                }
            }
            Model.DeliveryAddr delModel = delBLL.GetModel(_AddrId);
            if (delModel != null && delModel.UserId != DigitHelper.GetInt32(this.UserId))
            {
                return;
            }

            if (delModel == null)
            {
                delModel = new Model.DeliveryAddr();
                delModel.CreateTime = DateTime.Now;
                delModel.UserId = DigitHelper.GetInt32(this.UserId);
            }
            delModel.UpdateTime = DateTime.Now;
            delModel.Name = txtName.Text;
            delModel.Mobile = txtMobile.Text;
            delModel.PostCode = txtPostCode.Text;
            delModel.Address = txtAddress.Text;
            delModel.LocationId = DigitHelper.GetInt32(ddlCity.SelectedValue);
            delModel.IsMain = cbMain.Checked ? 1 : 0;
            delModel.IdentityNo = txtIdentityNo.Text.Trim();
            ImageUpload img = new ImageUpload();
            if (fileFront.Value != "")
            {
                img.SavePath = "~/uploadpic/identity/";
                img.FormFile = fileFront;
                img.Upload();
                if (img.Error == 0)
                {
                    delModel.IdentityFrontPic = img.OutFileName;
                }
            }
            if (fileBack.Value != "")
            {
                img.SavePath = "~/uploadpic/identity/";
                img.FormFile = fileBack;
                img.Upload();
                if (img.Error == 0)
                {
                    delModel.IdentityBackPic = img.OutFileName;
                }
            }
            if (_AddrId > 0)
            {
                delBLL.Update(delModel);
            }
            else
            {
                delModel.IsActive = 1;
                delBLL.Add(delModel);
            }

            Common.MessageBox.ShowAndRedirect(this.Page, "操作成功", "UserAddrList.aspx");
        }
    }
}